package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.ShoppingCart;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
       /* OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);*/
        return Result.success();
    }

    //历史订单查询
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> queryHistory(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("历史订单查询");
        PageResult pageResult = orderService.queryHistory(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    //查询订单详情
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> queryHistoryById(@PathVariable Long id){
        log.info("C端-查询订单详情");
        OrderVO orderVO = orderService.queryHistoryById(id);
        return Result.success(orderVO);
    }

    //取消订单  （设置状态）
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancellation(@PathVariable Long id){
        log.info("cancellation");
        orderService.cancellation(id);
        return Result.success();
    }

    //再来一单  (根据id查询历史订单添加入购物车）
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result anotherOrder(@PathVariable Long id){
        log.info("再来一单");
//        OrderVO orderVO = orderService.queryHistoryById(id);
        List<OrderDetail> orderDetail = orderService.queryDetailById(id);
        for(OrderDetail detail : orderDetail){
            ShoppingCartDTO shoppingCartDTO = new ShoppingCartDTO();
            BeanUtils.copyProperties(detail,shoppingCartDTO);
            shoppingCartService.addShoppingCart(shoppingCartDTO);
        }
        return Result.success();
    }

    //催单
    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result reminder(@PathVariable Long id){
        log.info("催单");
        orderService.reminder(id);
        return Result.success();
    }
}